﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Collections.ObjectModel;

namespace WpfApplication17
{
    /// <summary>
    /// Lane.xaml 的交互逻辑
    /// </summary>
    public partial class Lane : UserControl
    {

        #region 变量
        private ObservableCollection<PointCollection> pointsList = new ObservableCollection<PointCollection>();

        #endregion

        #region 构造

        public Lane()
        {
            
            InitializeComponent();
            PointsList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PointsList_CollectionChanged);
        } 

        #endregion

        #region CLR 属性

        /// <summary>
        /// 获取或设置构成道路的点集合
        /// </summary>
        public ObservableCollection<PointCollection> PointsList
        {
            get { return pointsList; }
            set { pointsList = value; }
        }

        #endregion

        #region 依赖属性

        #region LaneDirectImage

        public string LaneDirectImage
        {
            get { return (string)GetValue(LaneDirectImageProperty); }
            set { SetValue(LaneDirectImageProperty, value); }
        }

        public static readonly DependencyProperty LaneDirectImageProperty =
            DependencyProperty.Register("LaneDirectImage", typeof(string), typeof(Lane), new PropertyMetadata(new PropertyChangedCallback(LaneDirectImagePropertyChanged)));

        private static void LaneDirectImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Lane lane = (Lane)d;
            if (lane != null && lane.LaneDirectImage != null)
            {
                lane.DramLaneDirectImage();
            }
        }
        
        #endregion

        #region IsChecked

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(Lane), new UIPropertyMetadata(false));
  
        #endregion

        #endregion

        #region 注册事件

        #region Event
        private static readonly RoutedEvent CheckedEvent = EventManager.RegisterRoutedEvent(
             "Checked",
             RoutingStrategy.Bubble,
             typeof(RoutedEventHandler), typeof(Lane));

        public event RoutedEventHandler Checked
        {
            add { AddHandler(CheckedEvent, value); }
            remove { RemoveHandler(CheckedEvent, value); }
        }
        #endregion

        #region Event
        private static readonly RoutedEvent UnCheckedEvent = EventManager.RegisterRoutedEvent(
             "UnChecked",
             RoutingStrategy.Bubble,
             typeof(RoutedEventHandler), typeof(Lane));

        public event RoutedEventHandler UnChecked
        {
            add { AddHandler(UnCheckedEvent, value); }
            remove { RemoveHandler(UnCheckedEvent, value); }
        }
        #endregion

        #endregion
 
        #region 更新道路

        void PointsList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (PointsList.Count == 2)
            {
                status.Points = Helper.ConcatReversePoints(PointsList[0], PointsList[1]);
                if (LaneDirectImage != null)
                {
                    DramLaneDirectImage();
                }
            }
        }

        void DramLaneDirectImage()
        {
            LaneDirectImagepanel.Children.Clear();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(LaneDirectImage, UriKind.RelativeOrAbsolute);
            bi.EndInit();
            PointCollection axlePoints = new PointCollection();
            for (int i = 0; i < PointsList[0].Count;i++ )
            {
                axlePoints.Add(new Point((PointsList[0][i].X + PointsList[1][i].X) / 2, (PointsList[0][i].Y + PointsList[1][i].Y) / 2));
            }
            double width = new Vector(PointsList[1][0].X-PointsList[0][0].X,PointsList[1][0].Y-PointsList[0][0].Y).Length;
            for (int i = 0; i < axlePoints.Count - 1;i++ )
            {
                if (i != axlePoints.Count - 1)
                {
                    Vector small = new Vector(axlePoints[i + 1].X - axlePoints[i].X, axlePoints[i + 1].Y - axlePoints[i].Y);
                    if (small.Length > bi.Height * 2)
                    {
                        Image img = new Image();
                        img.Source = bi;
                        TransformGroup tg = new TransformGroup();
                        if (bi.Width > width)
                        {
                            double delta = bi.Width / bi.Height;
                            tg.Children.Add(new ScaleTransform() { ScaleX = (width) / bi.Width, ScaleY = (width ) / bi.Width });
                        }
                        img.Opacity = 0.7;
                        Canvas.SetLeft(img, axlePoints[i].X+ 0.5* small.X-0.5*bi.Width);
                        Canvas.SetTop(img, axlePoints[i].Y + 0.5 * small.Y - 0.5 * bi.Height);
                        img.RenderTransformOrigin = new Point(0.5, 0.5);
                        tg.Children.Add(new RotateTransform()
                        {
                            Angle =180- Vector.AngleBetween(new Vector(axlePoints[i + 1].X - axlePoints[i].X, axlePoints[i + 1].Y - axlePoints[i].Y), new Vector(0, 1))
                        });
                        img.RenderTransform = tg;
                        LaneDirectImagepanel.Children.Add(img);
                    }
                }
            }
        }
        #endregion

        #region 道路状态

        private void status_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!IsChecked)
                status.Fill = Brushes.Yellow;
        }

        private void status_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!IsChecked)
                status.Fill = Brushes.Transparent;
        }

        private void status_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (IsChecked)
            {
                IsChecked = false;
                RaiseEvent(new RoutedEventArgs(UnCheckedEvent,false));
                status.Fill = Brushes.Transparent;
            }
            else
            {
                IsChecked = true;
                RaiseEvent(new RoutedEventArgs(CheckedEvent, true));
                status.Fill = Brushes.LightBlue;
            }
        }
        
        #endregion       
    }
}
